package com.game.centerserver.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.game.centerserver.common.Result;
import com.game.centerserver.infrastructure.entity.DstClusterIni;
import com.game.centerserver.infrastructure.entity.DstClusterToken;
import com.game.centerserver.infrastructure.entity.DstServerIni;
import com.game.centerserver.infrastructure.service.DstClusterIniService;
import com.game.centerserver.infrastructure.service.DstClusterTokenService;
import com.game.centerserver.infrastructure.service.DstServerIniService;
import com.game.centerserver.service.GenerateArchive;
import jakarta.annotation.Resource;
import org.ini4j.Ini;
import org.springframework.stereotype.Service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 生成存档的实现类
 * @Author: tian
 * @Date: 2024/11/10/18:28
 * @Description:
 */
@Service
public class GenerateArchiveImpl implements GenerateArchive {

    @Resource
    DstClusterIniService dstClusterIniService;

    @Resource
    DstServerIniService dstServerIniService;

    @Resource
    DstClusterTokenService dstClusterTokenService;

    @Override
    public String generateArchiveDirection(String uid, String archiveID) {
        Path root = Paths.get("archive").resolve(uid);
        File d = root.toFile();
        if (!d.exists()) {
            if (!d.mkdirs()) {
                return null;
            }
        } else {
            if (!d.isDirectory()) {
                System.err.println("generateArchiveDirection: archive existed and not director");
                return null;
            }
        }

        Path archivePath = root.resolve(archiveID);
        d = archivePath.toFile();
        if (d.exists() && d.isDirectory()) {
            if (!clearConfigFile(d)) return null;
            return archivePath.toString();
        }

        if (d.exists()) {d.delete();}
        if (!archivePath.resolve("Master").toFile().mkdirs())
        {
            return null;
        }
        if (!archivePath.resolve("Caves").toFile().mkdirs())
        {
            return null;
        }

        return archivePath.toString();
    }

    @Override
    public boolean clearConfigFile(File archiveDir) {
        if(!archiveDir.exists() || archiveDir.isFile()) {return false;}

        Path archivePath = archiveDir.toPath();

        archivePath.resolve("cluster.ini").toFile().delete();
        archivePath.resolve("cluster_token.txt").toFile().delete();

        File fsub = archivePath.resolve("Master").toFile();
        if (!fsub.exists() || !fsub.isDirectory()) {
            fsub.mkdir();
        } else {
            archivePath.resolve("Master").resolve("worldgenoverride.lua").toFile().delete();
            archivePath.resolve("Master").resolve("server.ini").toFile().delete();
            archivePath.resolve("Master").resolve("modoverrides.lua").toFile().delete();
        }

        fsub = archivePath.resolve("Caves").toFile();
        if (!fsub.exists() || !fsub.isDirectory()) {
            fsub.mkdir();
        } else {
            archivePath.resolve("Caves").resolve("worldgenoverride.lua").toFile().delete();
            archivePath.resolve("Caves").resolve("server.ini").toFile().delete();
            archivePath.resolve("Caves").resolve("modoverrides.lua").toFile().delete();
        }

        return true;
    }

    @Override
    public boolean generateConfigsToArchiveDirection(String archiveId, File archivePath) {
        if (!archivePath.exists()) {return false;}

        Path root = archivePath.toPath();

        Path MasterPath = root.resolve("Master");
        Path CavesPath = root.resolve("Caves");

        if (!MasterPath.toFile().exists()) {MasterPath.toFile().mkdirs();}
        if (!CavesPath.toFile().exists()) {CavesPath.toFile().mkdirs();}

        File clusterTokenFile = root.resolve("cluster_token.txt").toFile();

        File clusterIniFile = root.resolve("cluster.ini").toFile();

        File masterServerIniFile = MasterPath.resolve("server.ini").toFile();
        File masterWorldgenoverrideLua = MasterPath.resolve("worldgenoverride.lua").toFile();
        File masterModoverridesLua = MasterPath.resolve("modoverrides.lua").toFile();

        File cavesServerIniFile = CavesPath.resolve("server.ini").toFile();
        File cavesWorldgenoverrideLua = CavesPath.resolve("worldgenoverride.lua").toFile();
        File cavesModoverridesLua = CavesPath.resolve("modoverrides.lua").toFile();

        clusterTokenFile.delete();
        clusterIniFile.delete();
        masterServerIniFile.delete();
        masterWorldgenoverrideLua.delete();
        masterModoverridesLua.delete();
        cavesServerIniFile.delete();
        cavesWorldgenoverrideLua.delete();
        cavesModoverridesLua.delete();

        Boolean generateClusterTokenFile = generateClusterTokenFile(archiveId,clusterTokenFile);
        if (!generateClusterTokenFile){
            return false;
        }

        Boolean generateClusterIniFile = generateClusterIniFile(archiveId, clusterIniFile);
        if (!generateClusterIniFile){
            return false;
        }

        // 生成地上和洞穴的 worldgenoverride.lua脚本
        try(BufferedWriter writer = new BufferedWriter(new FileWriter(masterWorldgenoverrideLua))){
            writer.write("return {\n" +
                    "    override_enabled = true,\n" +
                    "    preset = \"SURVIVAL_TOGETHER\",\n" +
                    "    overrides = {},\n" +
                    "}");
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        try(BufferedWriter writer = new BufferedWriter(new FileWriter(cavesWorldgenoverrideLua))){
            writer.write("return {\n" +
                    "    override_enabled = true,\n" +
                    "    preset = \"SURVIVAL_TOGETHER\",\n" +
                    "    overrides = {},\n" +
                    "}");
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

        // TODO 根据mod列表生成 modoverrides.lua脚本   （后续完成）
        try {
            masterModoverridesLua.createNewFile();
            cavesModoverridesLua.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        //根据archiveId生成server.ini文件
        List<DstServerIni> dstServerIniList = dstServerIniService.queryByArchiveId(archiveId);
        for (DstServerIni dstServerIni : dstServerIniList) {
            if (dstServerIni.isMaster()) {
                Boolean generateServerIniFile = generateServerIniFile(dstServerIni, masterServerIniFile);
                if (!generateServerIniFile){
                    return false;
                }
            } else if (!dstServerIni.isMaster()) {
                Boolean generateServerIniFile = generateServerIniFile(dstServerIni, cavesServerIniFile);
                if (!generateServerIniFile){
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * 根据archiveId生成完整的存档
     * @param archiveId
     * @return
     */
    @Override
    public Result generateCompleteArchive(String archiveId,List<String> modList){
        //创建目录
        String uid = StpUtil.getLoginIdAsString();

        //根据archiveID生成cluster.ini文件
        File clusterIniFile = new File("archive/" + uid + "/" + archiveId + "/cluster.ini");
        if (!clusterIniFile.getParentFile().exists()){
            boolean mkdirs = clusterIniFile.getParentFile().mkdirs();
            if(!mkdirs){
                return Result.fail("生成cluster.ini时，创建目录失败");
            }
        }
        Boolean generateClusterIniFile = generateClusterIniFile(archiveId, clusterIniFile);
        if (!generateClusterIniFile){
            return Result.fail("生成cluster.ini文件失败");
        }

        // 生成地上和洞穴的 worldgenoverride.lua脚本
        File masterWorldgenoverrideLua = new File("archive/" + uid + "/" + archiveId + "/Master/worldgenoverride.lua");
        if (!masterWorldgenoverrideLua.getParentFile().exists()){
            boolean mkdirs = masterWorldgenoverrideLua.getParentFile().mkdirs();
            if(!mkdirs){
                return Result.fail("生成地上的worldgenoverride.lua文件时，创建目录失败");
            }
        }
        try(BufferedWriter writer = new BufferedWriter(new FileWriter(masterWorldgenoverrideLua))){
            writer.write("return {\n" +
                    "    override_enabled = true,\n" +
                    "    preset = \"SURVIVAL_TOGETHER\",\n" +
                    "    overrides = {},\n" +
                    "}");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("生成地上的worldgenoverride.lua文件失败");
        }
        // TODO 根据mod列表生成 modoverrides.lua脚本   （后续完成）

        //根据archiveId生成server.ini文件
        List<DstServerIni> dstServerIniList = dstServerIniService.queryByArchiveId(archiveId);
        for (DstServerIni dstServerIni : dstServerIniList) {
            if (dstServerIni.isMaster()) {
                File serverIniFile = new File("archive/" + uid + "/" + archiveId + "/Master/server.ini");
                if (!serverIniFile.getParentFile().exists()){
                    boolean mkdirs = serverIniFile.getParentFile().mkdirs();
                    if(!mkdirs){
                        return Result.fail("生成地上server.ini时，创建目录失败");
                    }
                }
                Boolean generateServerIniFile = generateServerIniFile(dstServerIni, serverIniFile);
                if (!generateServerIniFile){
                    return Result.fail("生成地上server.ini文件失败");
                }
            }
            if (!dstServerIni.isMaster()) {
                File cavesWorldgenoverrideLua = new File("archive/" + uid + "/" + archiveId + "/Caves/worldgenoverride.lua");
                if (!cavesWorldgenoverrideLua.getParentFile().exists()){
                    boolean mkdirs = cavesWorldgenoverrideLua.getParentFile().mkdirs();
                    if(!mkdirs){
                        return Result.fail("生成洞穴的worldgenoverride.lua文件时，创建目录失败");
                    }
                }
                try(BufferedWriter writer = new BufferedWriter(new FileWriter(cavesWorldgenoverrideLua))){
                    writer.write("return {\n" +
                            "    override_enabled = true,\n" +
                            "    preset = \"DST_CAVE\",\n" +
                            "    overrides = {},\n" +
                            "}");
                }catch (Exception e){
                    e.printStackTrace();
                    return Result.fail("生成洞穴的worldgenoverride.lua文件失败");
                }

                File serverIniFile = new File("archive/" + uid + "/" + archiveId + "/Caves/server.ini");
                if (!serverIniFile.getParentFile().exists()){
                    boolean mkdirs = serverIniFile.getParentFile().mkdirs();
                    if(!mkdirs){
                        return Result.fail("生成洞穴server.ini时，创建目录失败");
                    }
                }
                Boolean generateServerIniFile = generateServerIniFile(dstServerIni, serverIniFile);
                if (!generateServerIniFile){
                    return Result.fail("生成洞穴server.ini文件失败");
                }
            }
        }

        //根据archiveId生成cluster_token.txt文件
        File clusterTokenFile = new File("archive/" + uid + "/" + archiveId + "/cluster_token.txt");
        if (!clusterTokenFile.getParentFile().exists()){
            boolean mkdirs = clusterTokenFile.getParentFile().mkdirs();
            if(!mkdirs){
                return Result.fail("生成cluster_token.txt时，创建目录失败");
            }
        }
        Boolean generateClusterTokenFile = generateClusterTokenFile(archiveId,clusterTokenFile);
        if (!generateClusterTokenFile){
            return Result.fail("生成cluster_token.txt文件失败");
        }
        return Result.ok("成功生成存档");
    }

    /**
     * 根据id生成cluster.ini文件
     * @param archiveId
     * @param targetFile
     * @return
     */
    @Override
    public Boolean generateClusterIniFile(String archiveId,File targetFile) {
        DstClusterIni dstClusterIni = dstClusterIniService.queryByArchiveId(archiveId);
        Boolean isWriterSuccess = writerDSTClusterToIni(dstClusterIni, targetFile);
        return isWriterSuccess;
    }

    /**
     * 根据id生成server.ini文件
     * @param dstServerIni
     * @param targetFile
     * @return
     */
    @Override
    public Boolean generateServerIniFile(DstServerIni dstServerIni,File targetFile) {
        Boolean isWriterSuccess = writerDSTServerToIni(dstServerIni, targetFile);
        return isWriterSuccess;
    }

    /**
     * 根据id生成cluster_token.txt文件
     * @param archiveId
     * @param targetFile
     * @return
     */
    @Override
    public Boolean generateClusterTokenFile(String archiveId,File targetFile) {

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(targetFile))){
            DstClusterToken dstClusterToken = dstClusterTokenService.queryByArchiveId(archiveId);
            if (dstClusterToken!=null && dstClusterToken.getClusterToken() != null){
                writer.write(dstClusterToken.getClusterToken());
            }else{
                return false;
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 生成worldgenoverride.lua脚本
     * @param isCaves ture表示要生成洞穴的脚本，false表示要生成地面的脚本
     * @param targetFile
     * @return
     */
    @Override
    public Boolean generateWorldgenoverrideFile(Boolean isCaves,File targetFile) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(targetFile))){
            writer.write("return {\n");
            writer.write("  override_enabled = true,\n");
            if(isCaves){
                writer.write("  preset = \"DST_CAVE\",\n");
            }else{
                writer.write("  preset = \"SURVIVAL_TOGETHER\",\n");
            }
            writer.write("  overrides = {},\n");
            writer.write("}\n");
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 生成mod的配置文件
     * @param modList
     * @return
     */
    @Override
    public Boolean generateModOverridesFile(List<String> modList) {
        // TODO 生成mod配置文件
        return null;
    }

    /**
     * 读取cluster.ini文件
     * @param clusterIniFile
     * @return
     */
    @Override
    public DstClusterIni readClusterIni(File clusterIniFile){
        DstClusterIni dstCluster = new DstClusterIni();
        try {
            String sectionName = "";
            Ini ini = new Ini(clusterIniFile);
            sectionName = "MISC";
            dstCluster.setMaxSnapshots(Integer.parseInt(ini.get(sectionName,"max_snapshots")));
            dstCluster.setConsoleEnabled(Integer.parseInt(ini.get(sectionName,"console_enabled")));
            sectionName = "SHARD";
            dstCluster.setShardEnabled(Integer.parseInt(ini.get(sectionName,"shard_enabled")));
            dstCluster.setBindIp(ini.get(sectionName,"bind_ip"));
            dstCluster.setMasterIp(ini.get(sectionName,"master_ip"));
            dstCluster.setMasterPort(Integer.parseInt(ini.get(sectionName,"master_port")));
            dstCluster.setClusterKey(ini.get(sectionName,"cluster_key"));
            sectionName = "STEAM";
            dstCluster.setSteamGroupOnly(ini.get(sectionName,"steam_group_only"));
            dstCluster.setSteamGroupId(ini.get(sectionName,"steam_group_id"));
            dstCluster.setSteamGroupAdmins(ini.get(sectionName,"steam_group_admins"));
            sectionName = "NETWORK";
            dstCluster.setOfflineCluster(ini.get(sectionName,"offline_cluster"));
            dstCluster.setTickRate(ini.get(sectionName,"tick_rate"));
            dstCluster.setWhitelistSlots(ini.get(sectionName,"whitelist_slots"));
            dstCluster.setClusterPassword(ini.get(sectionName,"cluster_password"));
            dstCluster.setClusterName(ini.get(sectionName,"cluster_name"));
            dstCluster.setClusterDescription(ini.get(sectionName,"cluster_description"));
            dstCluster.setLanOnlyCluster(ini.get(sectionName,"lan_only_cluster"));
            dstCluster.setClusterIntention(ini.get(sectionName,"cluster_intention"));
            dstCluster.setAutosaverEnabled(Integer.parseInt(ini.get(sectionName,"autosaver_enabled")));
            sectionName = "GAMEPLAY";
            dstCluster.setMaxPlayers(Integer.parseInt(ini.get(sectionName,"max_players")));
            dstCluster.setPvp(Integer.parseInt(ini.get(sectionName,"pvp")));
            dstCluster.setGameMode(ini.get(sectionName,"game_mode"));
            dstCluster.setPauseWhenEmpty(Integer.parseInt(ini.get(sectionName,"pause_when_empty")));
            dstCluster.setVoteEnabled(Integer.parseInt(ini.get(sectionName,"vote_enabled")));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        return dstCluster;
    }

    /**
     * 读取server.ini文件
     * @param serverIniFile
     * @return
     */
    @Override
    public DstServerIni readServerIni(File serverIniFile){
        DstServerIni dstServer = new DstServerIni();
        try {
            String sectionName = "";
            Ini ini = new Ini(serverIniFile);
            sectionName = "SHARD";
            dstServer.setIsMaster(Integer.parseInt(ini.get(sectionName, "is_master")));
            dstServer.setName(ini.get(sectionName, "name"));
            dstServer.setId(Integer.parseInt(ini.get(sectionName, "id")));
            sectionName = "STEAM";
            dstServer.setAuthenticationPort(Integer.parseInt(ini.get(sectionName, "authentication_port")));
            dstServer.setMasterServerPort(Integer.parseInt(ini.get(sectionName, "master_server_port")));
            sectionName = "NETWORK";
            dstServer.setServerPort(Integer.parseInt(ini.get(sectionName, "server_port")));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return dstServer;
    }


    /**
     * 将DSTCluster类写到cluster.ini文件中
     * @param dstCluster
     * @param targetFile
     * @return
     */
    @Override
    public Boolean writerDSTClusterToIni(DstClusterIni dstCluster,File targetFile){

        try(BufferedWriter writer = new BufferedWriter(new FileWriter(targetFile))){
            writer.write("[MISC]\n");
            if(dstCluster.getMaxSnapshots() != null){
                writer.write("max_snapshots = " + dstCluster.getMaxSnapshots() + "\n");
            }
            if(dstCluster.getConsoleEnabled() != null){
                String enable = dstCluster.getConsoleEnabled()==0 ? "false" : "true";
                writer.write("console_enabled = " +
                        enable +
                        "\n");
            }
            writer.write("[SHARD]\n");
            if (dstCluster.getShardEnabled() != null){
                String enable = dstCluster.getShardEnabled()==0 ? "false" : "true";
                writer.write("shard_enabled = " + enable + "\n");
            }
            if (dstCluster.getBindIp() != null){
                writer.write("bind_ip = " + dstCluster.getBindIp() + "\n");
            }
            if (dstCluster.getMasterIp() != null){
                writer.write("master_ip = " + dstCluster.getMasterIp() + "\n");
            }
            if (dstCluster.getMasterPort() != null){
                writer.write("master_port = " + dstCluster.getMasterPort() + "\n");
            }
            if (dstCluster.getClusterKey() != null){
                writer.write("cluster_key = " + dstCluster.getClusterKey() + "\n");
            }

            writer.write("[STEAM]\n");
            if(dstCluster.getSteamGroupOnly() != null){
                writer.write("steam_group_only = " + dstCluster.getSteamGroupOnly() + "\n");
            }
            if(dstCluster.getSteamGroupId() != null){
                writer.write("steam_group_id = " + dstCluster.getSteamGroupId() + "\n");
            }
            if(dstCluster.getSteamGroupAdmins() != null){
                writer.write("steam_group_admins = " + dstCluster.getSteamGroupAdmins() + "\n");
            }

            writer.write("[NETWORK]\n");
            if(dstCluster.getOfflineCluster() != null){
                writer.write("offline_cluster = " + dstCluster.getOfflineCluster() + "\n");
            }
            if(dstCluster.getTickRate() != null){
                writer.write("tick_rate = " + dstCluster.getTickRate() + "\n");
            }
            if(dstCluster.getWhitelistSlots() != null){
                writer.write("whitelist_slots = " + dstCluster.getWhitelistSlots() + "\n");
            }
            if(dstCluster.getClusterPassword() != null){
                writer.write("cluster_password = " + dstCluster.getClusterPassword() + "\n");
            }
            if(dstCluster.getClusterName() != null){
                writer.write("cluster_name = " + dstCluster.getClusterName() + "\n");
            }
            if(dstCluster.getClusterDescription() != null){
                writer.write("cluster_description = " + dstCluster.getClusterDescription() + "\n");
            }
            if(dstCluster.getLanOnlyCluster() != null){
                writer.write("lan_only_cluster = " + dstCluster.getLanOnlyCluster() + "\n");
            }
            if(dstCluster.getClusterIntention() != null){
                writer.write("cluster_intention = " + dstCluster.getClusterIntention() + "\n");
            }
            if(dstCluster.getAutosaverEnabled() != null){
                String enable = dstCluster.getAutosaverEnabled()==0 ? "false" : "true";
                writer.write("autosaver_enabled = " + enable + "\n");
            }

            writer.write("[GAMEPLAY]\n");
            if(dstCluster.getMaxPlayers() != null){
                writer.write("max_players = " + dstCluster.getMaxPlayers() + "\n");
            }
            if(dstCluster.getPvp() != null){
                String enable = dstCluster.getPvp()==0 ? "false" : "true";
                writer.write("pvp = " + enable + "\n");
            }
            if(dstCluster.getGameMode() != null){
                writer.write("game_mode = " + dstCluster.getGameMode() + "\n");
            }
            if(dstCluster.getPauseWhenEmpty() != null){
                String enable = dstCluster.getPauseWhenEmpty()==0 ? "false" : "true";
                writer.write("pause_when_empty = " + enable + "\n");
            }
            if(dstCluster.getVoteEnabled() != null){
                String enable = dstCluster.getVoteEnabled()==0 ? "false" : "true";
                writer.write("vote_enabled = " + enable + "\n");
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

        return true;
    }

    /**
     * 将DSTServer类写到server.ini文件中
     * @param dstServer
     * @param targetFile
     * @return
     */
    @Override
    public Boolean writerDSTServerToIni(DstServerIni dstServer,File targetFile){
        try(BufferedWriter writer = new BufferedWriter(new FileWriter(targetFile))){
            writer.write("[SHARD]\n");
            if(dstServer.getIsMaster() != null){
                String enable = dstServer.getIsMaster()==0 ? "false" : "true";
                writer.write("is_master = " + enable + "\n");
            }
            if(dstServer.getName() != null){
                writer.write("name = " + dstServer.getName() + "\n");
            }
            if(dstServer.getId() != null){
                writer.write("id = " + dstServer.getId() + "\n");
            }
            writer.write("[STEAM]\n");
            if(dstServer.getAuthenticationPort() != null){
                writer.write("authentication_port = " + dstServer.getAuthenticationPort() + "\n");
            }
            if(dstServer.getMasterServerPort() != null){
                writer.write("master_server_port = " + dstServer.getMasterServerPort() + "\n");
            }
            writer.write("[NETWORK]\n");
            if(dstServer.getServerPort() != null){
                writer.write("server_port = " + dstServer.getServerPort() + "\n");
            }
            writer.write("[ACCOUNT]\n");
            if(dstServer.getEncodeUserPath() != null){
                String enable = dstServer.getEncodeUserPath()==0 ? "false" : "true";
                writer.write("encode_user_path = " + enable + "\n");
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

        return true;
    }


    /**
     * 往ini文件中添加节
     * @param iniFile
     * @param sectionName
     * @return
     */
    @Override
    public Boolean addSection(File iniFile, String sectionName) {
        try {
            Ini ini = new Ini(iniFile);
            ini.add(sectionName);
            ini.store(iniFile);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 往ini文件中添加或修改字段
     * @param iniFile
     * @param sectionName
     * @param fieldName
     * @param fieldValue
     * @return
     */
    @Override
    public Boolean addOrUpdateField(File iniFile, String sectionName, String fieldName, String fieldValue) {
        try {
            Ini ini = new Ini(iniFile);
            Ini.Section section = ini.get(sectionName);
            section.put(fieldName, fieldValue);
            ini.store(iniFile);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除ini文件节中的字段
     * @param iniFile
     * @param sectionName
     * @param fieldName
     * @return
     */
    @Override
    public Boolean deleteField(File iniFile, String sectionName, String fieldName) {
        try {
            Ini ini = new Ini(iniFile);
            Ini.Section section = ini.get(sectionName);
            section.remove(fieldName);
            ini.store(iniFile);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除ini文件的节
     * @param iniFile
     * @param sectionName
     * @return
     */
    @Override
    public Boolean deleteSection(File iniFile, String sectionName) {
        try {
            Ini ini = new Ini(iniFile);
            ini.remove(sectionName);
            ini.store(iniFile);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}
